package com.zfei.gfb.controller;


import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Page;
import com.zfei.gfb.bean.Result;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.query.*;
import com.zfei.gfb.query.h5.H5GoodsQuery;
import com.zfei.gfb.query.h5.H5GreenPlotQuery;
import com.zfei.gfb.query.h5.H5LandResourcesQuery;
import com.zfei.gfb.vo.*;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author fgh
 * @version 1.0
 */

@Api(tags = "H5主页接口")
@RestController
@RequestMapping("/h5/home")
public class H5HomeController {

    private static final Logger logger = LoggerFactory.getLogger(H5HomeController.class);

    @Resource
    private H5HouseController h5HouseController;

    @Resource
    private H5LandResourcesController h5LandResourcesController;

    @Resource
    private H5JobController h5JobController;

    @Resource
    private H5JobWantedController h5JobWantedController;

    @Resource
    private H5GoodsController h5GoodsController;

    @Resource
    private H5GreenPlotController h5GreenPlotController;

    @Resource
    private H5NoticeController h5NoticeController;

    @Resource
    private H5BannerController h5BannerController;

    @ApiOperation(value = "所有村子")
    @ApiResponses({
            @ApiResponse(code = 200, message = "数组", response = GfbVillageEnumVo.class),
    })
    @LogResult
    @HttpJson("/village/list")
    public Object listVillages() {
        ArrayList<GfbVillageEnumVo> vos = new ArrayList<>();
        for (Const.DaiCunTwon value : Const.DaiCunTwon.values()) {
            GfbVillageEnumVo vo = new GfbVillageEnumVo();
            vo.setName(value.getName());
            vos.add(vo);
        }

        return vos;
    }

    @ApiOperation(value = "致富广场")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SquareVo.class),
    })
    @LogResult
    @HttpJson("/square/list")
    public Object listSquare(@RequestBody Page page) {

        Integer limit = page.getLimit() == null ? 2 : page.getLimit();
        Integer pageNo = page.getPageNo() == null ? 1 : page.getPageNo();

        SquareVo result = new SquareVo();
        //房屋租赁

        GfbHouseQuery houseQuery = new GfbHouseQuery();
        houseQuery.setLimit(limit);
        houseQuery.setPageNo(pageNo);
        Result houseResult = (Result) h5HouseController.list(houseQuery);
        if (houseResult.getCode() == Status.OK.value()) {
            GfbHouseListVo houseListVo = (GfbHouseListVo) (houseResult.getData());
            result.setHouseVoList(houseListVo);
        }


        //土地承租

        H5LandResourcesQuery landQuery = new H5LandResourcesQuery();
        landQuery.setLimit(limit);
        landQuery.setPageNo(pageNo);
        Result landResult = (Result) h5LandResourcesController.list(landQuery);
        if (landResult.getCode() == Status.OK.value()) {
            GfbLandResourcesListVo landListVo = (GfbLandResourcesListVo) (landResult.getData());
            result.setLandVoList(landListVo);
        }


        //求职

        GfbJobWantedQuery jobWantedQuery = new GfbJobWantedQuery();
        jobWantedQuery.setLimit(limit);
        jobWantedQuery.setPageNo(pageNo);
        Result jobWantedResult = (Result) h5JobWantedController.list(jobWantedQuery);
        if (jobWantedResult.getCode() == Status.OK.value()) {
            GfbJobWantedListVo jobWantedListVo = (GfbJobWantedListVo) (jobWantedResult.getData());
            result.setJobWantedVoList(jobWantedListVo);
        }


        // 招聘

        GfbJobQuery jobQuery = new GfbJobQuery();
        jobQuery.setLimit(limit);
        jobQuery.setPageNo(pageNo);
        Result jobResult = (Result) h5JobController.list(jobQuery);
        if (jobResult.getCode() == Status.OK.value()) {
            GfbJobListVo jobListVo = (GfbJobListVo) (jobResult.getData());
            result.setJobVoList(jobListVo);
        }

        //乡村集市

        H5GoodsQuery goodsQuery = new H5GoodsQuery();
        goodsQuery.setLimit(limit);
        goodsQuery.setPageNo(pageNo);
        Result goodsResult = (Result) h5GoodsController.list(goodsQuery);
        if (goodsResult.getCode() == Status.OK.value()) {
            GfbGoodsListVo goodsListVo = (GfbGoodsListVo) (goodsResult.getData());
            result.setGoodsVoList(goodsListVo);
        }


        //菜地认领

        H5GreenPlotQuery greenPlotQuery = new H5GreenPlotQuery();
        greenPlotQuery.setLimit(limit);
        greenPlotQuery.setPageNo(pageNo);
        Result greenPlotResult = (Result) h5GreenPlotController.list(greenPlotQuery);
        if (greenPlotResult.getCode() == Status.OK.value()) {
            GfbGreenPlotListVo greenPlotListVo = (GfbGreenPlotListVo) (greenPlotResult.getData());
            result.setGreenPlotVoList(greenPlotListVo);
        }

        return result;
    }


    @ApiOperation(value = "首页信息流")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = InfoList.class),
    })
    @LogResult
    @HttpJson("/homeStream")
    public Object homeStream(@RequestBody Page page) {

        InfoList result = new InfoList();
        List<InfoVo> infoVos = new ArrayList<>();
        SquareVo squareVo = (SquareVo) listSquare(page);
        boolean flag = true;

        Iterator<GfbHouseVo> houseVoIterator = squareVo.getHouseVoList().getData().iterator();
        Iterator<GfbLandResourcesVo> landVoIterator = squareVo.getLandVoList().getData().iterator();
        GfbJobListVo jobVoList = squareVo.getJobVoList();
        Iterator<GfbJobVo> jobVoIterator;
        if (jobVoList == null) {
            jobVoIterator = new ArrayList<GfbJobVo>().iterator();
        } else {
            if (jobVoList.getData() == null) {
                jobVoIterator = new ArrayList<GfbJobVo>().iterator();
            } else {
                jobVoIterator = jobVoList.getData().iterator();
            }
        }

        Iterator<GfbJobWantedVo> jobWantedVoIterator = squareVo.getJobWantedVoList().getData().iterator();
        Iterator<GfbGoodsVo> goodsVoIterator = squareVo.getGoodsVoList().getData().iterator();
        Iterator<GfbGreenPlotVo> greenPlotVoIterator = squareVo.getGreenPlotVoList().getData().iterator();

        int total = 0;
        total += squareVo.getHouseVoList() == null ? 0 : squareVo.getHouseVoList().getTotal();
        total += squareVo.getLandVoList() == null ? 0 : squareVo.getLandVoList().getTotal();
        total += squareVo.getJobVoList() == null ? 0 : squareVo.getJobVoList().getTotal();
        total += squareVo.getJobWantedVoList() == null ? 0 : squareVo.getJobWantedVoList().getTotal();
        total += squareVo.getGoodsVoList() == null ? 0 : squareVo.getGoodsVoList().getTotal();
        total += squareVo.getGreenPlotVoList() == null ? 0 : squareVo.getGreenPlotVoList().getTotal();

        while (flag) {
            InfoVo infoVo = new InfoVo();

            flag = false;

            if (houseVoIterator.hasNext()) {
                flag = true;
                infoVo.setHouseVo(houseVoIterator.next());
            }

            if (landVoIterator.hasNext()) {
                flag = true;
                infoVo.setLandVo(landVoIterator.next());
            }

            if (jobVoIterator.hasNext()) {
                flag = true;
                infoVo.setJobVo(jobVoIterator.next());
            }

            if (jobWantedVoIterator.hasNext()) {
                flag = true;
                infoVo.setJobWantedVo(jobWantedVoIterator.next());
            }

            if (goodsVoIterator.hasNext()) {
                flag = true;
                infoVo.setGoodsVo(goodsVoIterator.next());
            }

            if (greenPlotVoIterator.hasNext()) {
                flag = true;
                infoVo.setGreenPlotVo(greenPlotVoIterator.next());
            }
            if (flag) {
                infoVos.add(infoVo);
            }
        }
        result.setTotal(total);
        result.setData(infoVos);
        return result;
    }

    @ApiOperation(value = "首页")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = HomeVo.class),
    })
    @LogResult
    @HttpJson("/home")
    public Object home() {

        HomeVo result = new HomeVo();
        //
        result.setGfbVillageEnumVo((List<GfbVillageEnumVo>) listVillages());

        Result noticeResult = (Result) h5NoticeController.list(new GfbNoticeQuery());
        if (noticeResult.getCode() == Status.OK.value()) {
            result.setGfbNoticeListVo((GfbNoticeListVo) (noticeResult.getData()));
        }
        Result bannerResult = (Result) h5BannerController.list();
        if (bannerResult.getCode() == Status.OK.value()) {
            result.setGfbBannerListVo((GfbBannerListVo) (bannerResult.getData()));
        }

        Page page = new Page();
        page.setPageNo(1);
        page.setLimit(5);
        result.setSquareVo((SquareVo) listSquare(page));

        return result;
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("666");
        while (true) {
            Iterator<String> iterator = strings.iterator();
            if (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }

    }
}
